//
//  LFBaseTabBar.m
//  LoveFreshBee
//
//  Created by 王忠迪 on 10/15/16.
//  Copyright © 2016 王忠迪. All rights reserved.
//

#import "LFBaseTabBar.h"
#import "LFButton.h"

#define kWidth [UIScreen mainScreen].bounds.size.width / 4

@interface LFBaseTabBar ()

//购物车按钮
@property (nonatomic,strong) LFButton *cartBtn;

@end

@implementation LFBaseTabBar


- (LFButton *)cartBtn {
    
    if (nil == _cartBtn) {
        
        _cartBtn = [[LFButton alloc] initWithFrame:CGRectMake(2 * kWidth, 0, kWidth, 49)];
        
        _cartBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        
        _cartBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [_cartBtn setTitle:@"购物车" forState:UIControlStateNormal];
        
        [_cartBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [_cartBtn setImage:[UIImage imageNamed:@"shopCart"] forState:UIControlStateNormal];
        
        [_cartBtn setImage:[UIImage imageNamed:@"shopCart_r"] forState:UIControlStateHighlighted];
        
    }
    
    return _cartBtn;
}

- (void)btnClick {
    
    self.block();
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.cartBtn];
        
        [_cartBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return self;
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat height = self.bounds.size.height;
    
    __block NSInteger index = 0 ;
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            obj.frame = CGRectMake(index * kWidth, 0, kWidth, height);
            
            index += index == 1 ? 2 : 1;
        }
        
    }];
    
}

@end
